/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package traffic.light.simulator.town.cars;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import traffic.light.simulator.town.Town;

/**
 * This class is a class that manage the ambulance
 * Every 10 secs it makes pop an Ambulance in the town
 * It is implemented as a runnable class to make it as a thread
 *
 */
public class AmbulanceManager implements Runnable {
    private Town t;
    private boolean continu;
    
    public AmbulanceManager(Town t) {
        super();
        this.continu = true;
        this.t = t;
    }
    
    @Override
    public void run() {
        while(continu) {
            try {
                Thread.sleep(10000);
                if(new Random().nextBoolean() && t.numberOfAmbulance() <= 3) {
                    Ambulance a = new Ambulance(t);
                    t.initCarInTown(a);
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(AmbulanceManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public void setContinu(boolean continu) {
        this.continu = continu;
    }
}
